package com.easondongh.spring;

import com.easondongh.spring.config.AppConfig;
import com.easondongh.spring.service.CustomTypeConverter;
import com.easondongh.spring.service.OrderService;
import com.easondongh.spring.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

/**
 * @author donghao
 * @date 2021/12/26 20:27
 */
public class Application {

    public static void main(String[] args) {
        // 使用ClassPathXmlApplicationContext注意pom里面不能配置<packaging>pom</packaging>
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
//        CustomApplicationContext context = new CustomApplicationContext(AppConfig.class);
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//        context.getBeanFactory().setTypeConverter(new CustomTypeConverter());
        context.register(AppConfig.class);
        context.refresh();

        // 生成一个BeanDefinition对象，并设置beanClass为UserService.class，并注册到ApplicationContext中
//        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
//        beanDefinition.setBeanClass(UserService.class);
//        context.registerBeanDefinition("userService", beanDefinition);

        OrderService orderService = (OrderService) context.getBean("orderService");
        orderService.userServiceTest();

//        UserService userService = (UserService) context.getBean("userService");
//        userService.test();
//        OrderService userService1 = context.getBean("userService", OrderService.class);
//        userService1.test();

        System.out.println(Arrays.toString(OrderService.class.getDeclaredConstructors()));

    }

}

class CustomApplicationContext extends AnnotationConfigApplicationContext {

    public CustomApplicationContext(Class<AppConfig> appConfigClass) {
        super(appConfigClass);
    }

    @Override
    protected void initPropertySources() {
        super.initPropertySources();
//        super.getEnvironment().setRequiredProperties("xxx");
    }

    @Override
    protected void onRefresh() throws BeansException {
        super.onRefresh();
//        super.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
//            @Override
//            public void onApplicationEvent(ApplicationEvent event) {
//                System.out.println(event);
//            }
//        });
//        this.publishEvent(new PayloadApplicationEvent<>("Event", "Test event publish"));
    }
}
